import { Toast } from 'vant'

export const useClipboardText = () => {
  async function copyText(
    text,
    msg = '文本已复制到剪贴板',
  ) {
    try {
      const textarea = document.createElement('textarea')
      textarea.value = text
      document.body.appendChild(textarea)
      textarea.select()
      document.execCommand('copy')
      document.body.removeChild(textarea)
      Toast(msg)
    } catch (error) {
      console.error(error)
    }
  }

  return {
    copyText
  }
}
